package com.hqd.ch03.v49.boot.context.config;

import com.hqd.ch03.v49.boot.env.PropertySourceLoader;
import com.hqd.ch03.v49.utils.StringUtils;

public class StandardConfigDataReference {

    /**
     * 配置文件路径信息
     */
    private final ConfigDataLocation configDataLocation;
    /**
     * 配置文件路径
     */
    private final String resourceLocation;
    /**
     * 配置文件所在文件夹
     */
    private final String directory;
    /**
     * 多环境
     */
    private final String profile;

    /**
     * 配置加载器
     */
    private final PropertySourceLoader propertySourceLoader;


    public StandardConfigDataReference(ConfigDataLocation configDataLocation, String directory, String root, String profile,
                                       String extension, PropertySourceLoader propertySourceLoader) {
        this.configDataLocation = configDataLocation;
        String profileSuffix = (StringUtils.hasText(profile)) ? "-" + profile : "";
        this.resourceLocation = root + profileSuffix + ((extension != null) ? "." + extension : "");
        this.directory = directory;
        this.profile = profile;
        this.propertySourceLoader = propertySourceLoader;
    }

    public ConfigDataLocation getConfigDataLocation() {
        return this.configDataLocation;
    }

    String getResourceLocation() {
        return this.resourceLocation;
    }

    boolean isMandatoryDirectory() {
        return !this.configDataLocation.isOptional() && this.directory != null;
    }

    String getDirectory() {
        return this.directory;
    }

    String getProfile() {
        return this.profile;
    }

    boolean isSkippable() {
        return this.configDataLocation.isOptional() || this.directory != null || this.profile != null;
    }

    public PropertySourceLoader getPropertySourceLoader() {
        return this.propertySourceLoader;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if ((obj == null) || (getClass() != obj.getClass())) {
            return false;
        }
        StandardConfigDataReference other = (StandardConfigDataReference) obj;
        return this.resourceLocation.equals(other.resourceLocation);
    }

    @Override
    public int hashCode() {
        return this.resourceLocation.hashCode();
    }

    @Override
    public String toString() {
        return this.resourceLocation;
    }

}
